<?php
final class Customer {
	private $customer_id;
	private $firstname;
	private $lastname;
	private $email;
	private $telephone;
	private $fax;
	private $newsletter;
	private $customer_group_id;
	private $address_id;
	
  	public function __construct($registry) {
		$this->config = $registry->get('config');
		$this->db = $registry->get('db');
		$this->request = $registry->get('request');
		$this->session = $registry->get('session');
				
		if (isset($this->session->data['customer_id'])) { 
	              $customers  = $this->db->query("SELECT * FROM" . DB_PREFIX . " crm_customer WHERE id = '" . $this->session->data['customer_id'] . "'");             
                      $customer = $customers->rows;  
                      if($customer){
                      $bean_ids = $this->db->query("SELECT * FROM" . DB_PREFIX . " email_addr_bean_rel WHERE bean_id = '" .$customer[0]['id']. "'");
                      $bean_id = $bean_ids->rows;  
                      $customer_emails = $this->db->query("SELECT * FROM" . DB_PREFIX . " email_addresses WHERE id = '" . $bean_id[0]['email_address_id'] . "'");
                      $customer_email = $customer_emails->rows;                       
                      if($customer_email){
				$this->customer_id = $customer[0]['id'];
				$this->firstname = $customer[0]['first_name'];
				$this->lastname = $customer[0]['last_name'];
				$this->email = $customer_email[0]['email_address'];
				$this->telephone = $customer[0]['phone_mobile'];
				$this->fax = $customer[0]['phone_fax']; 
                      }
                      else {
				$this->logout();
			}
                      }
          }
        
        }
        public function login($email, $password) {
               $customer_emails = $this->db->query("SELECT * FROM" . DB_PREFIX . " email_addresses WHERE LOWER(email_address) = '" . $this->db->escape(strtolower($email)) . "'");
               $customer_email = $customer_emails->rows;  
               if(count($customer_email))
               {
               $bean_ids = $this->db->query("SELECT * FROM" . DB_PREFIX . " email_addr_bean_rel WHERE email_address_id = '" .$customer_email[0]['id']. "'");
               $bean_id = $bean_ids->rows;
               }
               else {
                   $bean_id = NULL;
               }
               if(count($bean_id))
               {
               $customer_passed  = $this->db->query("SELECT * FROM" . DB_PREFIX . " crm_customer WHERE id = '" . $bean_id[0]['bean_id'] . "'");             
               $customer_pass = $customer_passed->rows;
               }
              else {
                   $customer_pass = NULL;
              }
//cap phat mot session de luu lai thong tin nguoi dung trong mot phien giao dich,do do khi ta da dang nhap thi thi trang web no dang lam viec phuc vu cho cai session do
//khi ta logout thi session do se bi huy va se ko con phien lam viec voi session nay nua
//cap phat mot bien session               
               if(isset($customer_pass[0]['password'])&&(isset($customer_email[0]['email_address']))&&($customer_pass[0]['password'] == $password))	
               {
                    $this->session->data['customer_id'] = $customer_pass[0]['id'];
                $this->customer_id = $customer_pass[0]['id'];
                $this->firstname = $customer_pass[0]['first_name'];
                $this->lastname = $customer_pass[0]['last_name'];
                $this->email = $email;
                $this->telephone = $customer_pass[0]['phone_mobile'];
                $this->fax = $customer_pass[0]['phone_fax'];
	  		return true;
               }
    	 else {
      		return false;
    	}                                       
  	}
  	
	public function logout() {
		unset($this->session->data['customer_id']);

		$this->customer_id = '';
		$this->firstname = '';
		$this->lastname = '';
		$this->email = '';
		$this->telephone = '';
		$this->fax = '';
		$this->newsletter = '';
		$this->customer_group_id = '';
		$this->address_id = '';
  	}
  
  	public function isLogged() {
    	return $this->customer_id;
  	}

  	public function getId() {
    	return $this->customer_id;
  	}
      
  	public function getFirstName() {
		return $this->firstname;
  	}
  
  	public function getLastName() {
		return $this->lastname;
  	}
  
  	public function getEmail() {
		return $this->email;
  	}
  
  	public function getTelephone() {
		return $this->telephone;
  	}
  
  	public function getFax() {
		return $this->fax;
  	}
	
  	public function getNewsletter() {
		return $this->newsletter;	
  	}

  	public function getCustomerGroupId() {
		return $this->customer_group_id;	
  	}
	
  	public function getAddressId() {
		return $this->address_id;	
  	}
	
  	public function getBalance() {
		$query = $this->db->query("SELECT SUM(amount) AS total FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$this->customer_id . "'");
	
		return $query->row['total'];
  	}	
		
  	public function getRewardPoints() {
		$query = $this->db->query("SELECT SUM(points) AS total FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$this->customer_id . "'");
	
		return $query->row['total'];	
  	}	
}
?>